package com.pureut.storage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.storage.dto.*;
import com.pureut.storage.export.PurchaseReceiptExport;
import com.pureut.storage.service.PurchaseReceiptService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.order.api.vo.PurchaseOrderTraceVo;
import com.sale.storage.api.dto.PurchaseOrderTrackDto;
import com.sale.storage.api.vo.UpdateInvoicedQuantityVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/***
 * 采购入库控制层
 * Author:C
 * Date:2022/12/23 10:16
 * @DESC
 */
@RestController
@RequestMapping("/purchase-receipt")
public class PurchaseReceiptController extends BaseController {

    @Resource
    PurchaseReceiptService purchaseReceiptService;

    /**
     * 获取采购入库分页列表
     */
    //@SaCheckPermission("warehouse:purchase:list")
    @GetMapping("/list")
    public TableDataInfo<PurchaseReceiptDto> getList(PurchaseReceiptVo purchaseReceiptVo, PageQuery pageQuery) throws Exception {
        //查询采购入库列表
        return purchaseReceiptService.getReceiptList(purchaseReceiptVo, pageQuery);
    }

    /**
     * 新增采购入库
     */
    //@SaCheckPermission("warehouse:warehousing:purchase:add")
    @Log(title = "新增采购入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody PurchaseReceiptVo purchaseReceiptVo) throws Exception {
        return toAjax(purchaseReceiptService.addReceipt(purchaseReceiptVo));
    }

    /**
     * 批次到货入库
     */
    @Log(title = "批次到货入库", businessType = BusinessType.INSERT)
    @PostMapping("/batchArrival")
    public R<Void> batchArrival(@RequestBody PurchaseOrderDto purchaseOrderDto) throws Exception {
        return toAjax(purchaseReceiptService.batchArrival(purchaseOrderDto));
    }

    /**
     * 关结采购入库
     */
    //@SaCheckPermission("warehouse:warehousing:purchase:closing")
    @Log(title = "关结采购入库", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public R<Void> close(@RequestBody PurchaseReceiptVo purchaseReceiptVo) {
        return toAjax(purchaseReceiptService.close(purchaseReceiptVo));
    }

    /**
     * 查看
     */
    //@SaCheckPermission("warehouse:warehousing:purchase:query")
    @GetMapping("/view/{id}")
    public R<PurchaseReceiptDto> view(@PathVariable("id") Long id) {
        return R.ok(purchaseReceiptService.getView(id));
    }

    /**
     * 修改采购入库
     */
    //@SaCheckPermission("warehouse:warehousing:purchase:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Void> updateReceipt(@Validated @RequestBody PurchaseReceiptVo purchaseReceiptVo) {
        return toAjax(purchaseReceiptService.updateReceipt(purchaseReceiptVo));
    }

    /**
     * 删除
     */
    //@SaCheckPermission("warehouse:warehousing:purchase:remove")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public R<Void> deleteReceipt(@PathVariable("idStr") String idStr) {
        return toAjax(purchaseReceiptService.deleteReceipt(idStr));
    }

    /**
     * 采购入库交接
     */
    //@SaCheckPermission("warehouse:warehousing:other:handover")
    @PostMapping("/handover")
    @Log(title = "采购入库交接", businessType = BusinessType.UPDATE)
    public R<PickingRestHandoverDto> handover(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return R.ok(purchaseReceiptService.handover(pickingRestDbVo));
    }


//    /**
//     * 入库/送检
//     */
//    //@SaCheckPermission("warehouse:warehousing:purchase:deliver")
//    @Log(title = "入库/送检", businessType = BusinessType.INSERT)
//    @PostMapping("/warehousInspection")
//    public AjaxResult warehousInspection(@RequestBody WarehousInspectionVo warehousInspectionVo) throws Exception {
//        return toAjax(purchaseReceiptService.warehousInspection(warehousInspectionVo));
//    }

    /**
     * 导出
     */
    //@SaCheckPermission("warehouse:warehousing:purchase:export")
    @PostMapping("/inspectionMessageExport")
    public R<Void> inspectionMessageExport(HttpServletResponse response, PurchaseReceiptVo purchaseReceiptVo) throws IOException {
        List<PurchaseReceiptExport> infoExport = purchaseReceiptService.getInfoExport(purchaseReceiptVo);
        ExcelUtil<PurchaseReceiptExport> excelUtil = new ExcelUtil<PurchaseReceiptExport>(PurchaseReceiptExport.class);
        excelUtil.excelWriter(response, infoExport, "采购入库信息.xlsx");
        return R.ok();
    }

    /**
     * 采购入库（pda）
     */
    //@SaCheckPermission("warehouse:warehousing:purchase:goStorage")
    @PostMapping("/warehousingPDA")
    @Log(title = "采购入库(pda)", businessType = BusinessType.UPDATE)
    public R<PickingRestDbDto> warehousingPda(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return R.ok(purchaseReceiptService.warehousingPda(pickingRestDbVo));
    }

//    /**
//     * 入库打印
//     */
//    @PostMapping("/storagePrinting")
//    @Log(title = "入库打印", businessType = BusinessType.UPDATE)
//    public R<Void> storagePrinting(@RequestBody StoragePrintingVo storagePrintingVo) throws Exception {
//        return toAjax(purchaseReceiptService.storagePrinting(storagePrintingVo));
//    }

    /**
     * 入库打印
     */
//    @RequiresPermissions("warehouse:warehousing:purchase:deliver")
    @Log(title = "入库打印", businessType = BusinessType.INSERT)
    @PostMapping("/warehousInspection")
    public R<Void> warehousInspection(@RequestBody WarehousInspectionVo warehousInspectionVo) throws Exception {
        return toAjax(purchaseReceiptService.warehousInspection(warehousInspectionVo));
    }

    /**
     * 财务模块修改采购入库、销售退货、销售发货、采购退货发票数量及核销金额
     */
    @PutMapping("/updateInvoicedQuantity")
    public boolean updateInvoicedQuantity(@RequestBody UpdateInvoicedQuantityVo updateInvoicedQuantityVo) {
        return purchaseReceiptService.updateInvoicedQuantity(updateInvoicedQuantityVo);
    }

    /**
     * 根据供应商编码获取该供应商总欠款
     */
    @GetMapping("/getBalance")
    public String getBalance(String supplierCode) {
        return purchaseReceiptService.getBalance(supplierCode);
    }

    /**
     * 财务-核销单（根据供应商编码获取对应数据）
     */
    @GetMapping("/getDataByCode")
    public TableDataInfo<ImparityDto> getDataByCode(GetDataByCodeVo getDataByCodeVo, PageQuery pageQuery) {
        return purchaseReceiptService.getDataByCode(getDataByCodeVo, pageQuery);
    }

    /**
     * 根据采购订单状态修改采购入库状态(feign调用)
     */
    @PostMapping("/updateReceiptStatus")
    public R<Void> updateReceiptStatus(@RequestBody UpdateReceiptStatusVo updateReceiptStatusVo) {
        return toAjax(purchaseReceiptService.updateReceiptStatus(updateReceiptStatusVo));
    }

    /**
     * 手持pda 入库列表
     */
    @GetMapping("/androidPdaList")
    public TableDataInfo<AndroidPdaDto> androidPdaList(int number, PageQuery pageQuery) {
        //查询采购入库列表
        return purchaseReceiptService.androidPdaList(number,pageQuery);
    }

    /**
     * 手持pda 查看接口
     */
    @GetMapping("/androidPdaView")
    public R<AndroidPdaViewDto> androidPdaView(int number, long id) {
        return R.ok(purchaseReceiptService.androidPdaView(number, id));
    }

    /**
     * 手持pda 拣货撤销列表
     */
    @GetMapping("/pickCancellationList")
    public TableDataInfo<AndroidPdaDto> pickCancellationList(int number, PageQuery pageQuery) {
        //查询采购入库列表
        return purchaseReceiptService.pickCancellationList(pageQuery, number);
    }

    /**
     * 手持pda 拣货完成
     */
    @PostMapping("/completionPickingAndroid")
    public R<Void> completionPickingAndroid(@RequestBody CompletionPickingVo completionPickingVo) throws Exception {
        return toAjax(purchaseReceiptService.completionPickingAndroid(completionPickingVo));
    }

    /**
     * 手持pda 拣货撤销
     */
    @PostMapping("/pickCancellationAndroid")
    public R<Void> pickCancellationAndroid(@RequestBody CompletionPickingVo completionPickingVo) {
        return toAjax(purchaseReceiptService.pickCancellationAndroid(completionPickingVo));
    }

    /**
     * 手持pda 交接列表
     */
    @GetMapping("/androidHandoverList")
    public TableDataInfo<AndroidHandoverPdaDto> androidHandoverList(int number, PageQuery pageQuery) {
        //查询交接列表
        return purchaseReceiptService.androidHandoverList(number, pageQuery);
    }

    /**
     * 手持pda 交接详情
     */
    @GetMapping("/androidHandoverView")
    public R<AndroidHandoverPdaDto> androidHandoverView(int number, long id) {

        return R.ok(purchaseReceiptService.androidHandoverView(number, id));
    }

    /**
     * 手持pda 交接
     */
    @PostMapping("/androidHandover")
    public R<PickingRestHandoverDto> androidHandover(@RequestBody AndroidHandoverVo androidHandoverVo) {
        return R.ok(purchaseReceiptService.androidHandover(androidHandoverVo));
    }

    /**
     * 采购订单跟踪查询
     */
    @GetMapping("/purchaseOrderTrackList")
    public TableDataInfo<PurchaseOrderTrackDto> purchaseOrderTrackList(PurchaseOrderTraceVo purchaseOrderTraceVo) {
        return purchaseReceiptService.purchaseOrderTrackList(purchaseOrderTraceVo);
    }

    /**
     * 采购统计 - 采购汇总表物料统计
     */
    @GetMapping("/materialStatList")
    public TableDataInfo<MaterialStatListDto> materialStatList(MaterialStatListVo materialStatListVo, PageQuery pageQuery) throws Exception {
        return purchaseReceiptService.materialStatList(materialStatListVo, pageQuery);
    }

    /**
     * 财务-核销单 通过获取未全部核销的采购入库单、采购退货单、其他支出单数据
     */
    @GetMapping("/getNoVerificationList")
    public TableDataInfo<ImparityDto> getNoVerificationList(GetDataByCodeVo getDataByCodeVo, PageQuery pageQuery) {
        return purchaseReceiptService.getNoVerificationList(getDataByCodeVo, pageQuery);
    }

    /**
     * 查询仓库单据数据（结账反结账使用）
     */
    @GetMapping("/closingCounterClosingDocument")
    public R<ClosingDocumentDto> closingCounterClosingDocument(String time) {
        return R.ok(purchaseReceiptService.closingCounterClosingDocument(time));
    }
}
